package com.kunlun.basedata.organize.service.impl;

import com.kunlun.basedata.organize.dao.IDepartmentDao;
import com.kunlun.basedata.organize.model.DepartmentModel;
import com.kunlun.basedata.organize.service.ICompanyService;
import com.kunlun.basedata.organize.service.IDepartmentService;
import com.kunlun.basedata.utils.CommonUtil;
import com.kunlun.common.model.Page;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 部门业务Service类
 */
@Service
@Transactional
public class DepartmentService implements IDepartmentService {

    private final Logger logger = LogManager.getLogger();

    @Autowired
    private IDepartmentDao departmentDao;

    @Autowired
    private ICompanyService companyService;

    @Override
    public List<DepartmentModel> getDepartmentList(DepartmentModel departmentModel) throws Exception {
        Map<String, Object> queryMap = CommonUtil.packageQueryMap(departmentModel, 0, 0);
        return getDepartmentList(queryMap);
    }

    @Override
    public Page<DepartmentModel> getDepartmentPage(DepartmentModel departmentModel, int currentPage, int pageSize) throws Exception {
        int startIndex = (currentPage - 1) * pageSize;
        Map<String, Object> queryMap = CommonUtil.packageQueryMap(departmentModel, startIndex, pageSize);
        int count = departmentDao.getDepartmentCount(queryMap);
        List<DepartmentModel> allDepartment = departmentDao.getDepartmentPage(queryMap);

        Page<DepartmentModel> page = new Page<>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(count);
        page.setRecords(allDepartment);
        return page;
    }

    @Override
    public void addDepartment(DepartmentModel departmentModel) throws Exception {
        departmentModel.setId(CommonUtil.generateUUID());
        departmentDao.addDepartment(departmentModel);
    }

    @Override
    public void updateDepartment(DepartmentModel departmentModel) throws Exception {
        departmentDao.updateDepartment(departmentModel);
    }

    @Override
    public void deleteDepartment(List<String> list) throws Exception {
        departmentDao.deleteDepartment(list);
    }

    @Override
    public List<DepartmentModel> getDepartmentList(Map<String, Object> queryMap) throws Exception {
        return departmentDao.getDepartmentList(queryMap);
    }
}
